<?php
/*
  $Id: videos.php $
  Mefobe Cart Solutions
  http://www.mefobemarket.com

  Copyright (c) 2009 Wuxi Elootec Technology Co., Ltd

  This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License v2 (1991)
  as published by the Free Software Foundation.
*/

    if (!class_exists('content')) {
        include('includes/classes/content.php');
    }
class toC_Videos_Admin
{
    function getData($id)
    {
        global $osC_Database;

        $Qvideos = $osC_Database->query('select v.*, c.*  from :table_videos v left join :table_content c on c.content_id = v.videos_id  where v.videos_id = :videos_id and c.content_type = "videos"');

        $Qvideos->bindTable(':table_videos', TABLE_VIDEOS);
        $Qvideos->bindTable(':table_content', TABLE_CONTENT);
        $Qvideos->bindInt(':videos_id', $id);
        $Qvideos->execute();

        $data = $Qvideos->toArray();

        $Qvideos->freeResult();        

        $product_categories_array = content::getContentCategories($id, 'videos');
        $data['categories_id'] = implode(',', $product_categories_array);
        $data['content_name'] = $data['title'];

        return $data;
    }

    function save($id = null, $data)
    {
        global $osC_Database, $osC_Image;

        $error = false;

        $osC_Database->startTransaction();

        if (is_numeric($id)) {
            $Qvideo = $osC_Database->query('update :table_videos set videos_description = :videos_description,youtube_id = :youtube_id,title = :title where videos_id = :videos_id');
            $Qvideo->bindInt(':videos_id', $id);
        } else {
            $Qvideo = $osC_Database->query('insert into :table_videos (videos_description,youtube_id,title) values (:videos_description,:youtube_id,:title)');
        }

        $Qvideo->bindTable(':table_videos', TABLE_VIDEOS);
        $Qvideo->bindValue(':videos_description', $data['videos_description']);
        $Qvideo->bindValue(':title', $data['content_name']);
        $Qvideo->bindValue(':youtube_id', $data['youtube_id']);
        $Qvideo->setLogging($_SESSION['module'], $id);
        $Qvideo->execute();

        if ($osC_Database->isError()) {
            $error = true;
        } else {
            if (is_numeric($id)) {
                $videos_id = $id;
            } else {
                $videos_id = $osC_Database->nextID();
            }
        }

        //content
        if ($error === false) {
            $error = !content::saveContent($id, $videos_id, 'videos', $data);
        }

        //content_to_categories
        if ($error === false) {
            $error = !content::saveContentToCategories($id, $videos_id, 'videos', $data);
        }
        
        if ($error === false) {
            $osC_Database->commitTransaction();
            osC_Cache::clear('sefu-videos');
            return true;
        }

        $osC_Database->rollbackTransaction();

        return false;
    }


    function delete($id)
    {
        global $osC_Database, $osC_Image;

        $osC_Database->startTransaction();

        $error = !content::deleteContent($id,'videos');

        if ($error === false) {
            $Qvideos = $osC_Database->query('delete from :table_videos where videos_id = :videos_id');
            $Qvideos->bindTable(':table_videos', TABLE_VIDEOS);
            $Qvideos->bindInt(':videos_id', $id);
            $Qvideos->setLogging($_SESSION['module'], $id);
            $Qvideos->execute();

            if ($osC_Database->isError()) {
                $error = true;
            }
        }

        if ($error == true) {
            $osC_Database->rollbackTransaction();
            return false;
        }

        $osC_Database->commitTransaction();
        osC_Cache::clear('sefu-videos');
        return true;
    }
}

?>
